package com.itheima.edy.info.manager.server;

import com.itheima.edy.info.manager.dao.TeacherDao;
import com.itheima.edy.info.manager.domain.Teacher;

public class TeacherServer {



    public static boolean addTeacherInfo(Teacher teacher) {
        return TeacherDao.addTeacherInfo(teacher);
    }

    public static boolean isExists(String id) {
        //调用库管的方法，把教师数组传回来（获取存储教师对象的数组）
        Teacher []teachers = TeacherDao.findAllTeacher();
        //假设id在数组中不存在
        boolean exists = false;
        //遍历数组获取每一个教师对象，判断工号是否存在
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            //判断条件是 教师数组不为空并且教师工号不存在
            if(teacher != null && teacher.getId().equals(id))
            {
                exists = true;
                break;
            }
        }
          return exists;
    }

    public static Teacher[] findAllTeacher() {
        //调用库管对象的findAllTeacher 获取教师对象数组
        Teacher[] allTeacher = TeacherDao.findAllTeacher();
        //判断数组中是否有教师信息（有：返回地址，没有：返回null）
        //数组中只要存在一个不是null的元素，就代表有教师信息
        boolean flag = false;
        for (int i = 0; i < allTeacher.length; i++) {
            Teacher teacher = allTeacher[i];
            if (teacher != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //有信息
            return allTeacher;
        } else {
            //没有信息
            return null;
        }
    }

    public static void deleteTeacherById(String delId) {
        TeacherDao.deleteTeacherById(delId);
    }

    public static void updateTeacher(String updateId,Teacher newTeach) {
        TeacherDao.updateTeacher(updateId,newTeach);
    }

}
